﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using DAL;
using System.IO;

namespace StudentManagerPro.Student
{
	public partial class StudentManage : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
            this.Query.queryEvent += this.Query111;
            //if (!IsPostBack)
            //{
            //    this.ddlClass.DataSource = new StudentClassService().GetStudentClass();
            //    this.ddlClass.DataTextField = "ClassName";
            //    this.ddlClass.DataValueField = "ClassId";
            //    this.ddlClass.DataBind();
            //}

            //this.ltaMsg.Text = "";
        }

        /// <summary>
        /// 4.编写响应的事件
        /// </summary>
        protected void Query111()
        {
            this.dlstuInfo.DataSource = new StudentService().GetStudent(this.Query.ClassName);
            this.dlstuInfo.DataBind();
        }

        ///// <summary>
        ///// 查询学员信息
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //protected void btnQuery_Click(object sender, EventArgs e)
        //{
        //    this.dlstuInfo.DataSource = new StudentService().GetStudent(this.ddlClass.SelectedItem.Text);
        //    this.dlstuInfo.DataBind();
        //}

        //删除学员
        protected void btnDel_Click(object sender, EventArgs e)
        {
            //从CommandArgument中获取要删除学员Id
            LinkButton lbtn = (LinkButton)sender;
            var studentId = lbtn.CommandArgument;
            //执行删除
            try
            {
                int result = new StudentService().DeleteStudentByStudentId(studentId);
                if (result == 1)
                {
                    //同步删除照片
                    if (File.Exists(Server.MapPath("~/Images/NewImages"+ studentId+".jpg")))
                    {
                        File.Delete(Server.MapPath("~/Images/NewImages" + studentId + ".jpg"));
                    }
                    //this.ltaMsg.Text = "<script type='text/javascript'>alert('删除成功!')</script>";
                    ////同步刷新(以后使用Jquery从前端删除)
                    //btnQuery_Click(null, null);
                   
                }
            }
            catch (Exception ex)
            {
                //this.ltaMsg.Text = "<script type='text/javascript'>alert('删除失败：" + ex.Message + "')</script>";
            }

        }

    }
}